/**
 * 
 */
package Parser;

import LexicalAnalyzer.Token;

/**
 * @author akanter, vcolegro
 *
 */
public class Terminal extends Symbol {
	
	
	public Terminal(String str){
		this(str, -1);
	}
	
	public Terminal(String str, int type){
		content = str;
		this.type = type;
	}
	
	public Terminal(Token t){
		content = t.toString();
		this.type = t.type;
		}
	/* 
	 * input: token to match to this symbol
	 * output: whether token matches this symbol
	 */
	@Override
	public boolean matches(Token t) {   //TODO not that useful honestly...
		if(t.type == this.type){
			if(t.type==0 || (t.type>=6 && t.type<9)){  //if token is a constant or id (i.e. id, real, hex, int)
				return true;
				//return (t.getContent().subSequence(0, t.getContent().indexOf('(')).matches( (this.content)); //started writing this, then realized it was unnecessary
			}
		}
		return t.getContent().equals(this.content);
	}

	public boolean matches(String str){
		if (content.contains("(")){
			return str.equals(this.content.substring(this.content.indexOf('(')+1, this.content.indexOf(')')));
		}
		else return str.equals(content);
	}
	
	/* (non-Javadoc)
	 * @see Parser.Symbol#isTerminal()
	 */
	@Override
	public boolean isTerminal() {
		return true;
	}
	
	/*
	 * toString()
	 */
	@Override
	public String toString(){
		return content;
	}
	
	/**
	 * @return Returns the actual value of the Symbol, e.g., "x" instead of "id(x)" for the identifier x
	 */
	public String getContent(){
		if(this.content.contains("(")){
			return this.content.substring(this.content.indexOf('(')+1, this.content.indexOf(')'));
		}
		else return this.content;
	}
	
}
